package com.cnd.scm.common.utils;

import java.security.MessageDigest;

/**
 * @version V1.0
 * @ClassName: PasswordEncoder
 * @Description:TODO(该类提供密码加密功能)
 * @author: 李启联
 * @date: 2019-08-19 14:55
 * @Copyright: Copyright (c), 2018-2019
 * 申明：本内容未经作者本人同意,禁止外泄以及用于其他的商业项目.
 */
public class PasswordEncoder {

    /**
     * 密码加密算法 枚举类
     */
    public enum EnCodeAlgorithms {
        MD5,
        SHA1,
        SHA256;
    }

    private Object salt; //盐值
    private EnCodeAlgorithms algorithm;//算法

    public PasswordEncoder(Object salt) {
        this(salt, EnCodeAlgorithms.MD5);
    }

    public PasswordEncoder(Object salt, EnCodeAlgorithms algorithm) {
        this.salt = salt;
        this.algorithm = algorithm;
    }

    /**
     * 加密
     * @param sourceStr 加密源字符串
     * @return 加密后面字符串
     */
    public String encode(String sourceStr) {
        MessageDigest md;
        try {
            switch (algorithm){
                case MD5:
                    md = MessageDigest.getInstance("MD5");
                    break;
                case SHA1:
                    md = MessageDigest.getInstance("SHA-1");
                    break;
                case SHA256:
                    md = MessageDigest.getInstance("SHA-256");
                    break;
                default:
                    md = MessageDigest.getInstance("MD5");
                    break;
            }
            md.update(mergePasswordAndSalt(sourceStr).getBytes("UTF-8"));
            byte byteData[] = md.digest();
            StringBuffer hexString = new StringBuffer();
            for (int i = 0; i < byteData.length; i++) {
                String hex = Integer.toHexString(0xff & byteData[i]);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (Exception e) {}
        return "";
    }

    /**
     * 密码匹配验证
     * @param encPass 密文
     * @param rawPass 明文
     * @return
     */
    public boolean matches(String encPass, String rawPass) {
        String pass1 = "" + encPass;
        String pass2 = encode(rawPass);

        return pass1.equals(pass2);
    }

    private String mergePasswordAndSalt(String password) {
        if (password == null) {
            password = "";
        }

        if ((salt == null) || "".equals(salt)) {
            return password;
        } else {
            return password + "{" + salt.toString() + "}";
        }
    }

    public static void main(String[] args) {
        String salt = "helloworld";
        PasswordEncoder encoderMd5 = new PasswordEncoder(salt);
        String encode = encoderMd5.encode("test");
        System.out.println(encode);
        //boolean passwordValid = encoderMd5.validPassword("1bd98ed329aebc7b2f89424b5a38926e", "test");
        System.out.println(new PasswordEncoder(salt).matches("083a8db3ff5b9b4ece3ef2bde03226c8","test"));

        PasswordEncoder encoderSha = new PasswordEncoder(salt, EnCodeAlgorithms.SHA256);
        String pass2 = encoderSha.encode("test");
        System.out.println(pass2);
        //boolean passwordValid2 = encoderSha.validPassword("1bd98ed329aebc7b2f89424b5a38926e", "test");
        //System.out.println(passwordValid2);
    }

}